package com.zsl.swing.redis.desktop.window.dialog;

import com.zsl.swing.redis.desktop.model.DbCountConfigEntity;
import com.zsl.swing.redis.desktop.model.NodeEntity;
import com.zsl.swing.redis.desktop.px.PxField;
import com.zsl.swing.redis.desktop.px.PxTextArea;
import com.zsl.swing.redis.desktop.type.BtnOptEnum;
import com.zsl.swing.redis.desktop.type.MainMenuEnum;
import com.zsl.swing.redis.desktop.utils.ConfigUtils;
import com.zsl.swing.redis.desktop.utils.DialogUtils;
import com.zsl.swing.redis.desktop.utils.LocationUtils;
import com.zsl.swing.redis.desktop.utils.StringUtils;
import com.zsl.swing.redis.desktop.window.ZslRedisDesktopMainWindow;
import com.zsl.swing.redis.desktop.window.builder.ConfirmCancelBtnPanelBuilder;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class DbCountConfigDialog extends JDialog implements ActionListener {

    private int width = 1000;

    private int height = 600;

    private final JTextField idField = new JTextField(40);

    private JTextField showNameField = new PxField(600);

//    private JIntegerField dbCountField = new JIntegerField(40);
    private JTextArea showIndexesField = new PxTextArea(600, 400);

    private NodeEntity nodeEntity;

    private DbCountConfigDialog(NodeEntity nodeEntity){
        super(ZslRedisDesktopMainWindow.getMainWindow());

        this.nodeEntity = nodeEntity;

        this.setTitle(MainMenuEnum.DB_COUNT_SET.getText());
        this.setSize(width,height);
        this.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);

        this.initPanel();
        LocationUtils.locateCenter(this, width, height);

        this.setVisible(true);
    }

    private void initPanel() {
        DbCountConfigEntity dbCountConfig = ZslRedisDesktopMainWindow.getDbCountConfig(this.nodeEntity);
        this.idField.setVisible(false);

        this.idField.setText(dbCountConfig.getUniqueId());
        this.showNameField.setText(this.nodeEntity.getShowName());
        this.showNameField.setEditable(false);
//        this.dbCountField.setInteger(dbCountConfig.getDbCount());
        this.showIndexesField.setText(dbCountConfig.getDbShowConfig());

        Box verticalBox = Box.createVerticalBox();

//        Box p1 = Box.createHorizontalBox();
//        p1.add(new JLabel("连接名称"));
//        p1.add(this.showNameField);
//
//        verticalBox.add(p1);

        verticalBox.add(new JLabel("连接名称"));
        verticalBox.add(this.showNameField);

//        verticalBox.add(new JLabel(MainMenuEnum.DB_COUNT_SET.getText()));
//        verticalBox.add(this.dbCountField);

//        Box p2 = Box.createHorizontalBox();
//        p2.add(new JLabel(MainMenuEnum.DB_INDEXES_SHOW.getText()));
//        p2.add(new JScrollPane(this.showIndexesField,JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER));
//        verticalBox.add(p2);


        verticalBox.add(new JLabel(MainMenuEnum.DB_INDEXES_SHOW.getText()));
        verticalBox.add(this.showIndexesField);


        verticalBox.add(ConfirmCancelBtnPanelBuilder.getInstance(this));

        JPanel panel = new JPanel();

//        panel.setLayout(new BorderLayout());
//        panel.add(verticalBox, BorderLayout.CENTER);
        panel.add(verticalBox);
        this.setContentPane(panel);
    }

    public static void showDialog(NodeEntity nodeEntity){
        new DbCountConfigDialog(nodeEntity);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        String command = e.getActionCommand();
        if(BtnOptEnum.CANCEL.getText().equals(command)){
            this.dispose();
        }else
        if(BtnOptEnum.CONFIRM.getText().equals(command)){
            String showDbIndexes = this.showIndexesField.getText();
            if(StringUtils.isEmpty(showDbIndexes)){
                DialogUtils.errorDialog(ZslRedisDesktopMainWindow.getMainWindow(),"数据库索引序列不能为空");
            }else{
                DbCountConfigEntity configEntity = new DbCountConfigEntity();
                configEntity.setUniqueId(this.nodeEntity.getUniqueId());
                configEntity.setShowName(this.nodeEntity.getShowName());
//                configEntity.setDbCount(dbCount);

//                String showDbIndexes = showDbIndexes;
                if(!StringUtils.isEmpty(showDbIndexes)){
                    configEntity.setDbShowConfig(showDbIndexes.trim());
                }

                boolean result = ConfigUtils.writeDbCountConfig(configEntity);
                if(result){
                    this.dispose();
                    DialogUtils.restartConfirmDialog(ZslRedisDesktopMainWindow.getMainWindow(),"设置数据库展示数量成功，重启后生效！");
                }else{
                    DialogUtils.errorDialog(ZslRedisDesktopMainWindow.getMainWindow(),"设置数据库展示数量失败！");
                }
            }
        }
    }
}
